package com.truyentranh.modules.ohvui;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabContentFactory;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.truyentranh.R;
import com.truyentranh.controls.AdsViewControl;
import com.truyentranh.controls.IOnComicChanged;
import com.truyentranh.controls.IOnFinalPage;
import com.truyentranh.controls.ImagePagerAdapter;
import com.truyentranh.controls.OnSaveComic;
import com.truyentranh.data.HaiBLMetalData;
import com.truyentranh.main.AdsBussiness;
import com.truyentranh.main.ComicBussiness;
import com.truyentranh.modules.haivl.HaiVLActivity;
import com.truyentranh.modules.haivl.HaiVlCommentActivity;
import com.truyentranh.network.APIRequestWorker;
import com.truyentranh.network.Request;
import com.truyentranh.network.RequestListener;
import com.truyentranh.requester.OhVuiRequester;
import com.truyentranh.utils.AppSettingParams;
import com.truyentranh.utils.FaceBookUtils;
import com.truyentranh.utils.Logger;
import com.truyentranh.utils.Utils;
import com.truyentranh.utils.sqllite.datasource.TopicHaiBlData;

public class OhVuiActivity extends Activity implements OnTabChangeListener, RequestListener, IOnFinalPage, IOnComicChanged {

	private TabHost tabHost;
	private ImagePagerAdapter imageComicAdapter;
	private FrameLayout topMenuBar;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		// Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.ohvui_activity_layout);
		init();

		tabHost = (TabHost) findViewById(android.R.id.tabhost);
		tabHost.setup();
		tabHost.setOnTabChangedListener(this);

		final TabWidget tabWidget = tabHost.getTabWidget();
		FrameLayout tabContent = tabHost.getTabContentView();

		View[] originalTextViews = new View[tabWidget.getTabCount()];
		for (int index = 0; index < tabWidget.getTabCount(); index++) {
			originalTextViews[index] = (View) tabWidget.getChildTabViewAt(index);
		}
		tabWidget.removeAllViews();

		for (int index = 0; index < tabContent.getChildCount(); index++) {
			tabContent.getChildAt(index).setVisibility(View.GONE);
		}

		for (int index = 0; index < originalTextViews.length; index++) {
			final View tabContentView = tabContent.getChildAt(index);
			String tag = "";
			String name = "";
			if (index == 0) {
				tag = "new";
				name = "Mới nhất";
			} else if (index == 1) {
				tag = "legend";
				name = "Huy�?n thoại";
			} else if (index == 2) {
				tag = "hot";
				name = "Nóng hổi";
			} else if (index == 3) {
				tag = "vote";
				name = "Bình ch�?n";
			}
			TabSpec tabSpec = tabHost.newTabSpec(tag);
			View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, tabWidget, false);
			TextView title = (TextView) tabIndicator.findViewById(R.id.title);
			title.setText(name);
//			ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
//			icon.setImageResource(resoureId);
			tabSpec.setIndicator(tabIndicator);
			tabSpec.setContent(new TabContentFactory() {
				public View createTabContent(String tag) {
					return tabContentView;
				}
			});
			tabHost.addTab(tabSpec);
		}
		
		topMenuBar = (FrameLayout) findViewById(R.id.top_menu_bar);
		TextView textView = (TextView) findViewById(R.id.titleActivity);
		textView.setText("Oh vui quá");

		Button moreFunction = (Button) findViewById(R.id.btnMoreFunctions);
		if (moreFunction != null) {
			moreFunction.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					openOptionsMenu();
				}
			});
		}
		Button back = (Button) findViewById(R.id.btnBackAcvitity);
		if (back != null) {
			back.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					finish();
				}
			});
		}
	}
	
	void doAdsByServer() {
		AdsBussiness.getInstance().doGetAdsFromServer(this, prView);
	}
	
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		int itemId = item != null ? item.getItemId() : -1;
		switch (itemId) {
		case 1:
			Logger.logMe("Save default");
			ComicBussiness.getInstance(OhVuiActivity.this).doSaveCommicToDefault(OhVuiActivity.this, imageComicAdapter.getCurrentData(), new OnSaveComic() {

				public void onError(int errorCode) {
					processError(errorCode);
				}

				public void onDone(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
					// TODO Auto-generated
					// method stub

				}

				public void onDoneWithNewTopic(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
					Logger.logMe("Sai flow, chuyen gi da xay ra duoi tang bussiness");
				}
			});
			break;
		case 2:
//			Logger.logMe("Save ");
//			ComicBussiness.getInstance(OhVuiActivity.this).doSaveCommic(OhVuiActivity.this, imageComicAdapter.getCurrentData(), new OnSaveComic() {
//
//				public void onError(int errorCode) {
//					processError(errorCode);
//				}
//
//				public void onDone(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//					// TODO Auto-generated
//					// method stub
//
//				}
//
//				public void onDoneWithNewTopic(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//					if (haiBlData != null) {						
//						showNotice("Thông báo", "�?ã dùng \"" + haiBlData.getName() + "\" làm chủ đ�? lưu mặc định.\nTip : Bạn có thể thay đổi"
//								+ " chủ đ�? mặc định này trong phần thiết lập của ứng dụng");
//					}
//				}
//			});
			Utils.downloadPhoto(this, 0, this, imageComicAdapter.getCurrentData().getImageUrl(), getResources());
			break;
		case 3:
			Logger.logMe("facebook");
			FaceBookUtils.doShareFaceBook(imageComicAdapter.getCurrentData(), OhVuiActivity.this);
			break;
		case 4:
			doRefreshPage();
			Logger.logMe("refresh");
			break;
		case 5 :
			FaceBookUtils.doShareApplication(this);
			break;
		case 6 :
			doComment(imageComicAdapter.getCurrentData());
			break;
		case 7 :
			Utils.setWallpaper(this, 0, this,  imageComicAdapter.getCurrentData().getImageUrl(), getResources());
			break;
		}
		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		if (ComicBussiness.getInstance(this).getDefaultComicTopic() != null && ComicBussiness.getInstance(this).getDefaultComicTopic().getId() != -1) {
			menu.add(1, 1, 1, "Lưu " + ComicBussiness.getInstance(this).getDefaultComicTopic().getName()).setIcon(R.drawable.ic_save_default);
		}
		menu.add(1, 2, 2, "Lưu").setIcon(R.drawable.ic_saved);
		menu.add(3, 3, 3, "Facebook").setIcon(R.drawable.ic_facebook);
		menu.add(4, 4, 4, "Tải lại").setIcon(R.drawable.ic_refresh);

		menu.add(5, 5, 5, "Chia sẻ App").setIcon(R.drawable.ic_share);
		menu.add(5, 6, 6, "Bình luận").setIcon(R.drawable.ic_comment);
		menu.add(6, 7, 5, "�?ặt hình n�?n").setIcon(R.drawable.ic_wall);
		return super.onCreateOptionsMenu(menu);
	}
	
	boolean isShowFunctionControl = true;
	OnClickListener onImageViewTouchTapConfirm = new OnClickListener() {

		public void onClick(View v) {

			if (isShowFunctionControl) {
				Animation animFadeOut = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_out);
				topMenuBar.setAnimation(animFadeOut);
				tabHost.getTabWidget().setAnimation(animFadeOut);
				isShowFunctionControl = false;
				tabHost.getTabWidget().setVisibility(View.GONE);
				topMenuBar.setVisibility(View.GONE);
			} else {
				Animation animFadeIn = AnimationUtils.loadAnimation(getApplicationContext(), android.R.anim.fade_in);
				topMenuBar.setAnimation(animFadeIn);
				tabHost.getTabWidget().setAnimation(animFadeIn);
				isShowFunctionControl = true;
				tabHost.getTabWidget().setVisibility(View.VISIBLE);
				topMenuBar.setVisibility(View.VISIBLE);
			}

		}
	};

	@Override
	protected void onDestroy() {
		if(imageComicAdapter != null){
			imageComicAdapter.clearListener();
		}
		tabHost.removeAllViewsInLayout();
		tabHost.removeAllViews();
		tabHost.destroyDrawingCache();
		super.onDestroy();
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			finish();
			return true;
		}

		return super.onKeyDown(keyCode, event);
	}

	void processError(int errorCode) {
		String error = "";
		switch (errorCode) {
		
		case OnSaveComic.CREATE_TOPIC_ERROR:
			error = "Không thể tạo được chủ đ�?.";
			break;
		case OnSaveComic.DB_NOT_FOUND:
			error = "Không tìm thấy tập tin dữ liệu.";
			break;
		case OnSaveComic.DEFAULT_TOPIC_NOTFOUND:
			error = "Không tìm thấy chủ đ�? mặc định.";
			break;
		case OnSaveComic.IMAGE_NOT_FOUND:
			error = "Không có truyện hiển thị.";
			break;
		case OnSaveComic.INSERT_COMIC_ERROR:
			error = "Không thể thêm truyện vào máy.";
			break;
		case OnSaveComic.TOPIC_NAME_INVAILD:
			error = "Tên chủ đ�? không đúng.";
			break;
		case OnSaveComic.TOPIC_NOTFOUND:
			error = "Không tìm thấy chủ đ�?.";
			break;
		}
		if(!error.equals("")){
			Toast.makeText(OhVuiActivity.this, error, 1000).show();
		}
	}

	void showNotice(String title, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("�?óng", null);

		AlertDialog notice = builder.create();
		notice.show();
	}

	void doRefreshPage() {
		onTabChanged(currentTab);
	}

//	private QuickAction mQuickAction;
//
//	void initActionBar() {
//		mQuickAction = QuickAction.getCommonActionBar(OhVuiActivity.this, ComicBussiness.getInstance(this).getDefaultComicTopic(), new OnActionItemClickListener() {
//
//			public void onItemClick(int pos) {
//				if (ComicBussiness.getInstance(OhVuiActivity.this).getDefaultComicTopic() == null) {
//					if (pos == 0) {
//						ComicBussiness.getInstance(OhVuiActivity.this).doSaveCommic(OhVuiActivity.this, imageComicAdapter.getCurrentData(), new OnSaveComic() {
//
//							public void onError(int errorCode) {
//								processError(errorCode);
//							}
//
//							public void onDone(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								// TODO Auto-generated method stub
//
//							}
//
//							public void onDoneWithNewTopic(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								if (haiBlData != null) {
//									initActionBar();
//									showNotice("Thông báo", "�?ã dùng \"" + haiBlData.getName() + "\" làm chủ đ�? lưu mặc định.\nTip : Bạn có thể thay đổi" + " chủ đ�? mặc định này trong phần thiết lập của ứng dụng");
//								}
//							}
//						});
//					} else if (pos == 1) {
//						FaceBookUtils.doShareFaceBook(imageComicAdapter.getCurrentData(), OhVuiActivity.this);
//					} else if (pos == 2) {
//						doRefreshPage();
//					}  else if(pos == 3){
//						FaceBookUtils.makeFriend(imageComicAdapter.getCurrentData(),OhVuiActivity.this);
//					}
//				} else {
//					if (pos == 0) {
//						ComicBussiness.getInstance(OhVuiActivity.this).doSaveCommicToDefault(OhVuiActivity.this, imageComicAdapter.getCurrentData(), new OnSaveComic() {
//
//							public void onError(int errorCode) {
//								processError(errorCode);
//							}
//
//							public void onDone(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								// TODO Auto-generated method stub
//
//							}
//
//							public void onDoneWithNewTopic(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								Logger.logMe("Sai flow, chuyen gi da xay ra duoi tang bussiness");
//							}
//						});
//					} else if (pos == 1) {
//						ComicBussiness.getInstance(OhVuiActivity.this).doSaveCommic(OhVuiActivity.this, imageComicAdapter.getCurrentData(), new OnSaveComic() {
//
//							public void onError(int errorCode) {
//								processError(errorCode);
//							}
//
//							public void onDone(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								// TODO Auto-generated method stub
//
//							}
//
//							public void onDoneWithNewTopic(HaiBLMetalData blMetalData, TopicHaiBlData haiBlData) {
//								if (haiBlData != null) {
//									initActionBar();
//									showNotice("Thông báo", "�?ã dùng \"" + haiBlData.getName() + "\" làm chủ đ�? lưu mặc định.\nTip : Bạn có thể thay đổi" + " chủ đ�? mặc định này trong phần thiết lập của ứng dụng");
//								}
//							}
//						});
//					} else if (pos == 2) {
//						FaceBookUtils.doShareFaceBook(imageComicAdapter.getCurrentData(), OhVuiActivity.this);
//					} else if (pos == 3) {
//						doRefreshPage();
//					}  else if(pos == 4){
//						FaceBookUtils.makeFriend(imageComicAdapter.getCurrentData(),OhVuiActivity.this);
//					}
//				}
//
//			}
//		});
//	}

	private String currentTab = "";
	int newPage = 1;
	int legendPage = 1;
	int hotPage = 1;
	int votePage = 1;

	long currentRequestId = -1;

	void init() {
	}

	void getNew(RequestListener listener) {
		Logger.logMe("requesting page = " + newPage);
		OhVuiRequester haiVLRequester = OhVuiRequester.getNew(newPage, listener);
		currentRequestId = AppSettingParams.nextRequestId();
		haiVLRequester.setRequestId(currentRequestId);
		APIRequestWorker.getInstance().addAPIRequest(haiVLRequester);
	}

	void getLegend(RequestListener listener) {
		Logger.logMe("requesting page = " + legendPage);
		OhVuiRequester haiVLRequester = OhVuiRequester.getLegend(legendPage, listener);
		currentRequestId = AppSettingParams.nextRequestId();
		haiVLRequester.setRequestId(currentRequestId);
		APIRequestWorker.getInstance().addAPIRequest(haiVLRequester);
	}

	void getHot(RequestListener listener) {
		Logger.logMe("requesting page = " + hotPage);
		OhVuiRequester haiVLRequester = OhVuiRequester.getHot(hotPage, listener);
		currentRequestId = AppSettingParams.nextRequestId();
		haiVLRequester.setRequestId(currentRequestId);
		APIRequestWorker.getInstance().addAPIRequest(haiVLRequester);
	}

	void getVote(RequestListener listener) {
		Logger.logMe("requesting page = " + votePage);
		OhVuiRequester haiVLRequester = OhVuiRequester.getVote(votePage, listener);
		currentRequestId = AppSettingParams.nextRequestId();
		haiVLRequester.setRequestId(currentRequestId);
		APIRequestWorker.getInstance().addAPIRequest(haiVLRequester);
	}

	private void doComment(final HaiBLMetalData blMetalData) {
		if (blMetalData != null) {
			FaceBookUtils.doViewComment(this, getApplication(), OhVuiCommentActivity.class, blMetalData.getDetailUrl());
		}
//		final Facebook facebook = new Facebook(GlobalData.FACEBOOK_APP_ID);
//		boolean isValid = SessionStore.restore(facebook, getApplicationContext());
//		if (isValid) {
//			Logger.logMe(">>>>>>>>>>Logged before");
//			Logger.logMe("getAccessToken " + facebook.getAccessToken());
//			Logger.logMe("getAccessExpires " + facebook.getAccessExpires());
//			Intent intent = new Intent(getApplication(), OhVuiCommentActivity.class);
//			if (blMetalData != null) {
//				intent.putExtra("IMAGE_ID", blMetalData.getDetailUrl());
//			}
//			startActivity(intent);
//		} else {
//			facebook.authorize(this, new DialogListener() {
//
//				public void onFacebookError(FacebookError e) {
//					// TODO Auto-generated method stub
//
//				}
//
//				public void onError(DialogError e) {
//					// TODO Auto-generated method stub
//
//				}
//
//				public void onComplete(Bundle values) {
//					if (facebook != null) {
//						// TODO Auto-generated method stub
//						Logger.logMe("getAccessToken " + facebook.getAccessToken());
//						Logger.logMe("getAccessExpires " + facebook.getAccessExpires());
//						SessionStore.save(facebook, getApplicationContext());
//						Intent intent = new Intent(getApplication(), OhVuiCommentActivity.class);
//						if (blMetalData != null) {
//							intent.putExtra("IMAGE_ID", blMetalData.getDetailUrl());
//						}
//						startActivity(intent);
//					}
//				}
//
//				public void onCancel() {
//					// TODO Auto-generated method stub
//
//				}
//			});
//		}
	}

	OnClickListener clickListener = new OnClickListener() {

		public void onClick(View v) {
			if (imageComicAdapter != null && imageComicAdapter.getCurrentData() != null) {
				String videoLink = imageComicAdapter.getCurrentData().getVideoLink();
				if (videoLink != null) {
					Logger.logMe("Link from server");
					Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(videoLink));
					startActivity(browserIntent);
				} else {
					String image = imageComicAdapter.getCurrentData().getImageUrl();
					if (image != null && image.indexOf("youtube") != -1) {
						String[] rs = image.split("/");
						if (rs.length > 0) {
							String id = rs[rs.length - 2];
							String url = "http://www.youtube.com/embed/" + id;
							Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
							startActivity(browserIntent);
						}
					}
				}
			}
		}
	};

	RequestListener listener = new RequestListener() {

		public void onRequestFailed(String errorMsg, Request request) {
			runOnUiThread(new Runnable() {
				public void run() {					
					imageComicAdapter.clearData();
					loading.setVisibility(View.GONE);
					btnShare.setVisibility(View.GONE);
					loadingError.setVisibility(View.VISIBLE);
					doAdsByServer();
				}
			});
		}

		public void onRequestComplete(Request request) {
			// TODO Auto-generated method stub
			if (request instanceof OhVuiRequester && currentRequestId == request.getRequestId()) {
				ArrayList<HaiBLMetalData> datas = ((OhVuiRequester) request).getDataList();
				imageComicAdapter.setNewData(datas);
				runOnUiThread(new Runnable() {
					public void run() {
						if(isShowFunctionControl){
							menuBar.setVisibility(View.VISIBLE);							
							imageDescription.setVisibility(View.VISIBLE);
						} else {
							menuBar.setVisibility(View.GONE);
							imageDescription.setVisibility(View.GONE);
						}
						
						loading.setVisibility(View.GONE);
						prView.setVisibility(View.VISIBLE);
						viewPager.setVisibility(View.VISIBLE);					
						btnShare.setVisibility(View.GONE);
						
						imageComicAdapter.notifyDataSetChanged();
						
						imageComicAdapter.onPageSelected(0);
						viewPager.invalidate();
						doAdsByServer();
					}
				});
			}
		}
	};

	private ViewPager viewPager = null;
	private AdsViewControl prView = null;
	private LinearLayout loading = null;
	private LinearLayout loadingError = null;
	private TextView imageDescription = null;
	private LinearLayout menuBar = null;
	private LinearLayout videoFunction = null;
	private TextView commentView = null;
	private Button btnView = null;
	LinearLayout loadingFinalPage = null;
	
	OnClickListener commentClick = new OnClickListener() {

		public void onClick(View v) {
			if (imageComicAdapter != null && imageComicAdapter.getCurrentData() != null) {
				doComment(imageComicAdapter.getCurrentData());
			}
		}
	};
	Button btnShare;
	public void onTabChanged(String tag) {
		View view = tabHost.getCurrentView();

		viewPager = (ViewPager) view.findViewById(R.id.view_pager);
		loadingFinalPage   = (LinearLayout) view.findViewById(R.id.loadingFinalPage);
		prView = (AdsViewControl) view.findViewById(R.id.prview);
		loading = (LinearLayout) view.findViewById(R.id.loadingpage);
		loadingError = (LinearLayout) view.findViewById(R.id.loadingError);
		imageDescription = (TextView) view.findViewById(R.id.imageDescription);
		menuBar = (LinearLayout) view.findViewById(R.id.menubar);
		videoFunction = (LinearLayout) view.findViewById(R.id.videoFunction);
		commentView = (TextView) view.findViewById(R.id.commentView);
		commentView.setOnClickListener(commentClick);
		btnView = (Button) view.findViewById(R.id.doViewVideoFunction);
		
		btnShare = (Button) findViewById(R.id.btnShare);
		btnShare.setVisibility(View.GONE);
		commentView.setVisibility(View.GONE);
		
		btnView.setOnClickListener(clickListener);

		viewPager.setVisibility(View.INVISIBLE);
		prView.setVisibility(View.INVISIBLE);
		loading.setVisibility(View.VISIBLE);
		menuBar.setVisibility(View.INVISIBLE);
		loadingError.setVisibility(View.INVISIBLE);
		
		currentTab = tag;
		if (imageComicAdapter == null) {
			imageComicAdapter = new ImagePagerAdapter(this);
			imageComicAdapter.setOnFinalPage(this);
			imageComicAdapter.setComicChanged(this);	
			imageComicAdapter.setOnClickImageViewSingerTapListener(onImageViewTouchTapConfirm);
		}
		imageComicAdapter.clearData();
		viewPager.setAdapter(imageComicAdapter);
		viewPager.setOnPageChangeListener(imageComicAdapter);

		Logger.logMe("Request tag = "+tag);
		if ("new".equals(tag)) {
			getNew(listener);
		} else if ("hot".equals(tag)) {
			getHot(listener);
		} else if ("legend".equals(tag)) {
			getLegend(listener);
		} else if ("vote".equals(tag)) {
			getVote(listener);
		}
	}

	public void onRequestComplete(Request request) {

	}

	public void onRequestFailed(String errorMsg, Request request) {
		// TODO Auto-generated method stub

	}

	RequestListener listenerOnFinalPage = new RequestListener() {

		public void onRequestFailed(String errorMsg, Request request) {
			// TODO Auto-generated method stub
			runOnUiThread(new Runnable() {
				public void run() {
					loadingFinalPage.setVisibility(View.GONE);
				}					
			});
		}

		public void onRequestComplete(Request request) {
			// TODO Auto-generated method stub
			if (request instanceof OhVuiRequester && currentRequestId == request.getRequestId()) {
				final ArrayList<HaiBLMetalData> datas = ((OhVuiRequester) request).getDataList();
				runOnUiThread(new Runnable() {
					public void run() {
						loadingFinalPage.setVisibility(View.GONE);
						if (imageComicAdapter != null) {
							imageComicAdapter.setMetalData(datas);
							imageComicAdapter.notifyDataSetChanged();
						}								
					}
				});
			}
		}
	};

	int countAds = 0;
	public void onFinalPage() {
		loadingFinalPage.setVisibility(View.VISIBLE);
		Logger.logMe(">>>> onFinalPage");
		countAds++;
//		if(countAds >= 2){
			countAds = 0;
			doAdsByServer();
//		}
		if ("new".equals(currentTab)) {
			newPage++;
			getNew(listenerOnFinalPage);
		} else if ("hot".equals(currentTab)) {
			hotPage++;
			getHot(listenerOnFinalPage);
		} else if ("legend".equals(currentTab)) {
			legendPage++;
			getLegend(listenerOnFinalPage);
		} else if ("vote".equals(currentTab)) {
			votePage++;
			getVote(listenerOnFinalPage);
		}
	}

	public void onFinalItem(HaiBLMetalData data) {
		// TODO Auto-generated method stub

	}

	public void commicChanged(HaiBLMetalData blMetalData) {		
		if (commentView != null) {
			String comment = getResources().getString(R.string.comment_string);
			if(!blMetalData.getCommentCount().equals(""))
				comment = blMetalData.getCommentCount() + " " + comment;
			commentView.setText(comment);
		}
		if (imageDescription != null) {
			imageDescription.setText(blMetalData.getTitleDescription());
		}
		if (videoFunction != null) {
			if (blMetalData.isVideo()) {
				videoFunction.setVisibility(View.VISIBLE);
			} else {
				videoFunction.setVisibility(View.INVISIBLE);
			}
		}
	}
}
